import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    keepAliveInclude: []
  },
  mutations: {
    updateKeepAliveInclude: (state, data) => {
      state.keepAliveInclude = data;
    },
    clearKeepAliveInclude: state => {
      state.keepAliveInclude = [];
    }
  },
  actions: {
    async postData({ commit, rootState }, { url, data, need_sign = true }) {
      try {
        if (typeof data === 'object' && process.env.VUE_APP_ENABLE_SIGN && need_sign) {
          let sign = md5(JSON.stringify(data));
          data.S = aes.encrypt(sign.toString().toUpperCase(), url).toString();
        }
        let res = await api.post(url, data);
        if (res && res.IsSuccess) {
          if (Array.isArray(res.Data)) {
            return {
              List: res.Data,
              Paging: res.Paging
            };
          } else if (res.Data && res.Data.Items && Array.isArray(res.Data.Items)) {
            return {
              Data: res.Data,
              Paging: res.Paging
            };
          } else {
            return res.Data;
          }
        } else {
          throw new Error(res.ErrorMsg);
        }
      } catch (error) {
        throw error;
      }
    }
  },
});
